package com.ruoyi.collection.process;

import com.alibaba.fastjson.JSON;
import com.ruoyi.base.yaml.BusinessConfig;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.service.ILoanIouService;
import com.ruoyi.collection.domain.*;
import com.ruoyi.collection.process.api.CollectionProcess;
import com.ruoyi.sms.service.ISmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Slf4j
@Service("CreateTransferToUs")
public class CreateTransferToUsPorcessor implements CollectionProcess {

    @Autowired
    private ISmsService smsService;

    @Autowired
    BusinessConfig businessConfig;

    @Autowired
    private ILoanIouService loanIouService;


    private static RestTemplate restTemplate;
    static {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(180000);
        factory.setReadTimeout(180000);
        restTemplate = new RestTemplate(factory);
    }

    @Override
    public Object process(ApiParameter req) throws Exception {

        CreateTransferToUsReq reqBody = JSON.parseObject(req.getBody().toString(), CreateTransferToUsReq.class);

        LoanIou loanIouByLoanSerialNo = loanIouService.getLoanIouByLoanSerialNo(reqBody.getSerialNo());

        Map<String, String> map = createTransferToUs(loanIouByLoanSerialNo.getId());

        CreateTransferToUsResp respBody = new CreateTransferToUsResp();
        respBody.setTransferToUs(map);

        return respBody;

    }

    public Map<String, String> createTransferToUs(Long id) {
        String url = businessConfig.getApiurl()+"/forcollection/getCreateTransferToUs?id="+id;
        log.info("url:::"+url);
        Map<String, String> map = restTemplate.getForObject(url,Map.class);
        log.info("map>>>>"+map.toString());
        return map;
    }
}
